package com.kmxd.ams.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kmxd.ams.client.system.ISysConfigSvc;
import com.kmxd.ams.client.system.dto.SysConfigAddDTO;
import com.kmxd.ams.client.system.dto.SysConfigEditDTO;
import com.kmxd.ams.client.system.dto.SysConfigQueryDTO;
import com.kmxd.ams.client.system.vo.SysConfigListVO;
import com.kmxd.ams.client.system.vo.SysConfigVO;
import com.kmxd.ams.core.common.enums.LogLevelEnum;
import com.kmxd.ams.core.common.enums.LogTypeEnum;
import com.kmxd.ams.core.log.annotation.AutoLog;
import com.kmxd.ams.core.result.R;
import com.kmxd.ams.infra.system.entity.SysConfig;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 系统配置 前端控制器
 *
 * @author admin
 * @since 2021-06-10
 */
@Tag(name = "系统配置")
@RestController
@RequestMapping("/system/sysConfig")
@AllArgsConstructor
public class SysConfigCtl {

  private ISysConfigSvc sysConfigSvc;

  /** 分页查询 */
  @GetMapping("/selectPage")
  @AutoLog(module = "参数管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "分页查询")
  public R<Page<SysConfigListVO>> selectPage(Page reqPage, SysConfigQueryDTO req) {
    return R.success(sysConfigSvc.selectPage(reqPage, req));
  }

  /** 查询所有 */
  @GetMapping("/selectAll")
  @AutoLog(module = "参数管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查询所有")
  public R<List<SysConfigListVO>> selectAll() {
    List<SysConfig> lists = sysConfigSvc.selectAll();
    List<SysConfigListVO> listsVo = BeanUtil.copyToList(lists, SysConfigListVO.class);
    return R.success(listsVo);
  }

  /** 新增 */
  @PostMapping("/add")
  @SaCheckPermission("system:sysConfig:add")
  @AutoLog(module = "参数管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "新增")
  public R<Void> add(@Validated @RequestBody SysConfigAddDTO req) {
    sysConfigSvc.add(req);
    return R.success();
  }

  /** 修改 */
  @PostMapping("/edit")
  @SaCheckPermission("system:sysConfig:edit")
  @AutoLog(module = "参数管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH, title = "修改")
  public R<Void> edit(@Validated @RequestBody SysConfigEditDTO req) {
    sysConfigSvc.edit(req);
    return R.success();
  }

  /** 删除 */
  @PostMapping("/remove")
  @SaCheckPermission("system:sysConfig:remove")
  @AutoLog(module = "参数管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH, title = "删除")
  public R<Void> remove(@RequestParam String ids) {
    sysConfigSvc.remove(ids);
    return R.success();
  }

  /** 查看 */
  @GetMapping("/view")
  @AutoLog(module = "参数管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查看")
  public R<SysConfigVO> view(@RequestParam Long id) {
    return R.success(sysConfigSvc.view(id));
  }

  /** 导出 */
  @GetMapping("/export")
  @SaCheckPermission("system:sysConfig:export")
  @AutoLog(module = "参数管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH, title = "导出")
  @SneakyThrows
  public void export(SysConfigQueryDTO req, HttpServletResponse response) {
    String filename = "参数列表" + "_" + System.currentTimeMillis() + ".xlsx";
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
    response.setHeader(
        "Content-Disposition",
        "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
    EasyExcel.write(response.getOutputStream())
        .head(SysConfigListVO.class)
        .sheet("参数列表")
        .doWrite(sysConfigSvc.selectList(req));
  }

  /** 根据键值，获取value */
  @GetMapping("/getConfigValue")
  @AutoLog(
      module = "参数管理",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.LOW,
      title = "根据键值，获取value")
  public R<String> getConfigValue(@RequestParam String key) {
    SysConfig sysConfig = sysConfigSvc.getConfigValueByKey(key);
    return R.success(
        ObjectUtil.isNotNull(sysConfig)
            ? StringEscapeUtils.unescapeJava(sysConfig.getContent())
            : null);
  }

  /** 刷新缓存 */
  @PostMapping("/refresh")
  @SaCheckPermission("system:sysConfig:refresh")
  @AutoLog(module = "参数管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "刷新缓存")
  public R<Void> refresh() {
    sysConfigSvc.clearAllCache();
    return R.success();
  }
}
